const {
  publishCourseErro,
  saveCourseInfoError,
  getCourseInfoError,
  getChaptersError,
  saveOrUpdateChapterErr,
  invalChapterId,
  getChapterByIdError
} = require('../constant/err.type')

const { createCourse } = require('../service/course.service')
const {
  saveCourseInfoSer,
  findAllCourses,
  findCourseInfoById,
  findAllChapter,
  testSer,
  findChAndLeByCourseId,
  findChapterById,
  updateChapter
} = require('../service/courses.service')

class CourseController {
  //直接调用service的createCourse方法
  async create(ctx) {
    try {
      await createCourse(ctx.request.body)
      ctx.body = {
        code: 0,
        message: '发布课程成功',
        data: ''
      }
    } catch (err) {
      //   console.err(err)
      return ctx.app.emit('err', publishCourseErro, ctx)
    }
  }
  //新增课程接口
  async saveCourseInfo(ctx) {
    let courseInfo = ctx.request.body

    // console.log(courseInfo)
    try {
      const res = await saveCourseInfoSer(courseInfo)

      let { id, title } = res

      ctx.body = {
        code: 0,
        message: '发布课程成功',
        data: res
      }
    } catch (err) {
      //console.log(err.sqlMessage)
      saveCourseInfoError.result = err
      return ctx.app.emit('error', saveCourseInfoError, ctx)
    }
  }

  //获取课程列表接口
  async getCourseList(ctx) {
    //1.解析pageNum和pageSize

    const { pageNum = 1, pageSize = 10 } = ctx.request.query

    console.log(ctx.request.query)

    //2.调用数据处理的方法
    const res = await findAllCourses(pageNum, pageSize)

    //3.返回结果
    ctx.body = {
      code: 0,
      message: '获取课程列表成功！',
      data: res
    }
  }

  //通过ID获取单门课程信息
  async getCourseInfoById(ctx) {
    try {
      const { id } = ctx.request.query
      const res = await findCourseInfoById(id)
      ctx.body = {
        code: 0,
        message: '获取课程信息成功!',
        data: res
      }
    } catch (err) {
      //console.log(err.sqlMessage)
      //console.log(err)
      getCourseInfoError.result = err
      return ctx.app.emit('error', getCourseInfoError, ctx)
    }
  }

  //获取所有章节信息
  async getAllChapter(ctx) {
    try {
      const res = await findAllChapter()
      console.log(res)

      let arr = []
      for (let i in res) {
        arr.push[i]
      }

      console.log(arr)
      ctx.body = {
        code: 0,
        message: '获取章节信息成功!',
        data: res
      }
    } catch (err) {
      getChaptersError.result = err
      return ctx.app.emit('error', getChaptersError, ctx)
    }
  }

  //获取所有章节和课时信息
  async getChAndLeByCourseId(ctx) {
    try {
      const { courseId } = ctx.request.query
      if (!courseId) {
        throw 'courseId 不存在！'
      }
      const res = await findChAndLeByCourseId(courseId)
      ctx.body = {
        code: 0,
        message: '通过课程id获取章节信息成功!',
        data: res
      }
    } catch (err) {
      getChaptersError.result = err
      return ctx.app.emit('error', getChaptersError, ctx)
    }
  }

  //通过ID获取章节信息
  async getChapterById(ctx) {
    try {
      const { id } = ctx.request.query
      if (!id) {
        throw 'id不存在！'
      }
      const res = await findChapterById(id)
      ctx.body = {
        code: 0,
        message: '通过ID获取章节信息成功!',
        data: res
      }
    } catch (err) {
      getChapterByIdError.result = err
      return ctx.app.emit('error', getChapterByIdError, ctx)
    }
  }

  async saveOrUpdateChapter(ctx) {
    let chapterInfo = ctx.request.body
    try {
      const res = await updateChapter(chapterInfo)
      if (res.id) {
        ctx.body = {
          code: 0,
          message: '章节新增成功！',
          data: res
        }
      } else if (res === true) {
        ctx.body = {
          code: 0,
          message: '章节更新成功！',
          data: res
        }
      } else if (res === false) {
        return ctx.app.emit('error', invalChapterId, ctx)
      }
    } catch (err) {
      saveOrUpdateChapterErr.result = err.parent.sqlMessage
      return ctx.app.emit('error', saveOrUpdateChapterErr, ctx)
    }
  }

  //用于测试sequelize
  async test(ctx) {
    const res = await testSer()

    ctx.body = res
  }
}

module.exports = new CourseController()
